/**  
 * 文 件 名: RightTypeAction.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

/**
 * 
 */
package com.tongzhou.system.rightsMgmt.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.system.rightsMgmt.dao.IRightTypeDAO;
import com.tongzhou.system.rightsMgmt.model.RightType;
import com.tongzhou.system.rightsMgmt.model.SearchRightTypeCondition;
import com.tongzhou.util.Util;

public class RightTypeAction extends ActionSupport{
	private String id;//
	private String name;//权限类型名称
	private String table_name;//表名
	private String create_time;//创建时间
	private String create_by;//创建人
	private String update_time;//更新时间
	private String update_by;//更新人
	
	private RightType rightType;
	private IRightTypeDAO dao;
	private List<RightType> result;
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private SearchRightTypeCondition condition;
	private String judgment;
	private String judgment2;
	
	public String listRightType() throws DBException{
		checkSearchCondition();
		condition = new SearchRightTypeCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setName(name);
		condition.setTable_name(table_name);
		condition.setCurrentPage(currentPage);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType);
		condition.setPageSize(pageSize);
	    result = dao.listRightType(condition);
		return "listRightType";
	}
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "r.id";
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "asc" ;
		}
	}
	public String toAddRightType() throws DBException{
		return "toAddRightType";
	}
	
	public String checkRightTypeByName() throws DBException{
		rightType=new RightType();
		try {
			rightType.setName(new String(name.getBytes("ISO-8859-1"),"UTF-8"));
			rightType.setId(id);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result=dao.checkRightTypeByName(rightType);
		if(result.size()>0){
			judgment="0";
		}else{
			judgment="1";
		}
		return SUCCESS;
	}
	
	public String checkRightTypeByTableName() throws DBException{
		rightType=new RightType();
		try {
			rightType.setTable_name(new String(table_name.getBytes("ISO-8859-1"),"UTF-8"));
			rightType.setId(id);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result=dao.checkRightTypeByTableName(rightType);
		if(result.size()>0){
			judgment2="0";
		}else{
			judgment2="1";
		}
		return SUCCESS;
	}
	
	public String addRightType() throws DBException{
		rightType=new RightType();
		rightType.setName(name);
		rightType.setTable_name(table_name);
		rightType.setCreate_by(String.valueOf(Util.getAdminUserId()));
		dao.addRightType(rightType);
		listRightType();
		return "listRightType";
	}
	
	public String deleteRightType() throws DBException{
		rightType=new RightType();
		rightType.setId(id);
		
		dao.deleteRightType(rightType);
		listRightType();
		name="";
		return "listRightType";
	}
	
	public String toEditRightType() throws DBException{
		rightType=new RightType();
		rightType.setId(id);
		rightType=dao.getRightTypeById(rightType);
		return "toEditRightType";
	}
	
	public String editRightType() throws DBException{
		rightType=new RightType();
		rightType.setId(id);
		rightType.setName(name);
		rightType.setTable_name(table_name);
		rightType.setUpdate_by(String.valueOf(Util.getAdminUserId()));
		dao.updateRightType(rightType);
		listRightType();
		return "listRightType";
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTable_name() {
		return table_name;
	}
	public void setTable_name(String tableName) {
		table_name = tableName;
	}
	public String getCreate_time() {
		return create_time;
	}
	public void setCreate_time(String createTime) {
		create_time = createTime;
	}
	public String getCreate_by() {
		return create_by;
	}
	public void setCreate_by(String createBy) {
		create_by = createBy;
	}
	public String getUpdate_time() {
		return update_time;
	}
	public void setUpdate_time(String updateTime) {
		update_time = updateTime;
	}
	public String getUpdate_by() {
		return update_by;
	}
	public void setUpdate_by(String updateBy) {
		update_by = updateBy;
	}
	public RightType getRightType() {
		return rightType;
	}
	public void setRightType(RightType rightType) {
		this.rightType = rightType;
	}
	public IRightTypeDAO getDao() {
		return dao;
	}
	public void setDao(IRightTypeDAO dao) {
		this.dao = dao;
	}
	public List<RightType> getResult() {
		return result;
	}
	public void setResult(List<RightType> result) {
		this.result = result;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public String getOrderByType() {
		return orderByType;
	}
	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}
	public SearchRightTypeCondition getCondition() {
		return condition;
	}
	public void setCondition(SearchRightTypeCondition condition) {
		this.condition = condition;
	}
	public String getJudgment() {
		return judgment;
	}
	public void setJudgment(String judgment) {
		this.judgment = judgment;
	}
	public String getJudgment2() {
		return judgment2;
	}
	public void setJudgment2(String judgment2) {
		this.judgment2 = judgment2;
	}
}
